/*******************************************************************************
*   Copyright (C) Harbin Institute of Technology, Shenzhen.
*				  All Rights Reserved.
*   Department: Decoration
********************************************************************************
* File Name   : timer.c
* Author      : Dayuan
* Version     : v0.01
* Date        : 2019/9/2
* Description : Decoration Rototics Manipulator task.
*******************************************************************************/
/*-History----------------------------------------------------------------------
* Version   Date      Name        Changes and comments
* v0.01     2019/9/2  Dayuan    initial version
*=============================================================================*/
/* System Includes -----------------------------------------------------------*/
#include <stdint.h>
/* Public Includes -----------------------------------------------------------*/
#include "tim.h"
#include "main.h"
#include "encoder_if.h"
/* Private Includes ----------------------------------------------------------*/
#include "timer_if.h"
#include "timer.h"
/* Global variables ----------------------------------------------------------*/
struct SYS_TIME sys_time;
uint16_t sys_timer_tick_counter;  

/* Private variables ---------------------------------------------------------*/
uint8_t timer_tick_poll_counter;


/* Global functions ----------------------------------------------------------*/
/**
  * @brief  config system timer interrupt.
  * @param  None
  * @retval None
  */
void sys_time_init(void)
{
	sys_time.nb_sec     = 0;
	sys_time.nb_sec_rem = 0;
	sys_time.nb_tick    = 0;

	sys_time.ticks_per_sec = TIM7_PWM_FREQ;
	sys_time.resolution = 1.0 / sys_time.ticks_per_sec;

	sys_time.cpu_ticks_per_sec = TIM7_FREQ / TIM7_PRESCALAR;
	/* cpu ticks per desired sys_time timer step */
  sys_time.resolution_cpu_ticks = (uint32_t)(sys_time.resolution * sys_time.cpu_ticks_per_sec + 0.5f);

}

/**
  * @brief  system timer isr handler.
  * @param  None
  * @retval None
  */
void sys_timer_isr_handler(void)
{
	sys_time_polling();
	encoder_polling();
}


void sys_time_polling(void)
{
	sys_time.nb_tick++;
	sys_time.nb_sec_rem += sys_time.resolution_cpu_ticks;
	if (sys_time.nb_sec_rem >= sys_time.cpu_ticks_per_sec) 
	{
	   sys_time.nb_sec_rem -= sys_time.cpu_ticks_per_sec;
	   sys_time.nb_sec++;
  }
}

int32_t max_timer_error(int32_t usec)
{
	static int32_t max_time;
	if(usec > max_time)
		max_time = usec;
	return max_time;
}
/**
  * @brief  reset force timer counter.
  * @param  None
  * @retval None
  */
uint32_t force_timer_reset(void)
{
	HAL_TIM_Base_Stop_IT(&htim6);
	return __HAL_TIM_SET_COUNTER(&htim6,0);
}

/**
  * @brief  get system tim current counter.
  * @param  None
  * @retval None
  */
uint32_t systick_get_value(void)
{
	return __HAL_TIM_GET_COUNTER(&htim7);
}
/**
  * @brief  delay us.
  * @param  None
  * @retval None
  */
void delay_us(uint16_t us)
{
	uint32_t i = 30*us;
	while(i--)
	{
		__asm__("nop");
	}
}

/**
  * @brief  delay ms.
  * @param  None
  * @retval None
  */
void delay_ms(uint16_t ms)
{
	while(ms--)
	{
		delay_us(1000);
	}
}

/**************** END OF FILE *****************************************/
